/*
 *
 * Copyright 2001 Sun Microsystems, Inc. All Rights Reserved.
 *
 * This software is the proprietary information of Sun
 * Microsystems, Inc.  Use is subject to license terms.
 *
 */
/**
 * The SimpleTopicPublisher class consists only of a main method,
 * which publishes several messages to a topic.
 *
 * Run this program in conjunction with SimpleTopicSubscriber.
 * Specify a topic name on the command line when you run the
 * program.  By default, the program sends one message.
 * Specify a number after the topic name to send that number
 * of messages.
 */
package sbc.jms;

import java.util.Properties;
import javax.jms.JMSException;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import sbc.fabrik.model.Feature;
import sbc.fabrik.model.Platine;
import sbc.fabrik.model.Sensor;

public class MozartBridge {
    
    
    /**
     * Main method.
     *
     * @param args     the topic used by the example and,
     *                 optionally, the number of messages to send
     */
    public static void main(String[] args) {
        Context                 jndiContext = null;
        QueueConnectionFactory  queueConnectionFactory = null;
        QueueConnection              queueConnection = null;
        QueueSession            queueSession = null;
        Queue queue = null;
        ObjectMessage             message = null;
        final int               NUM_MSGS;
        
        
        /*
         * Create a JNDI InitialContext object if none exists
         * yet.
         */
        try {
            Properties properties = new Properties();
            properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
            properties.put(Context.URL_PKG_PREFIXES, "org.jnp.interfaces");
            properties.put(Context.PROVIDER_URL, "localhost");
            
            
            
            jndiContext = new InitialContext(properties);
        } catch (NamingException e) {
            System.out.println("Could not create JNDI " + "context: " + e.toString());
            e.printStackTrace();
            System.exit(1);
        }
        
        /*
         * Look up connection factory and topic.  If either does
         * not exist, exit.
         */
        try {
            queueConnectionFactory = (QueueConnectionFactory) jndiContext.lookup("ConnectionFactory");
            queue = (Queue) jndiContext.lookup("queue/sbc");
        } catch (NamingException e) {
            System.out.println("JNDI lookup failed: " + e.toString());
            e.printStackTrace();
            System.exit(1);
        }
        
        /*
         * Create connection.
         * Create session from connection; false means session is
         * not transacted.
         * Create publisher and text message.
         * Send messages, varying text slightly.
         * Finally, close connection.
         */
        try {
            queueConnection = queueConnectionFactory.createQueueConnection();
            QueueSession session = (QueueSession) queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
            QueueSender publisher = session.createSender(queue);
            
            
            
            for (int i=0; i < 10; i++) {
                message = session.createObjectMessage();
                
                Platine platine = new Platine(i);
                message.setObject(platine);
                message.setStringProperty("ModulType", platine.getClass().getCanonicalName());
                
                
                System.out.println("Publishing message: " + message.getObject().toString());
                publisher.send(message);
                
                
                message = session.createObjectMessage();
                
                Sensor sensor = new Sensor(i*i);
                sensor.addFeature(Feature.RADIATION);
                sensor.addFeature(Feature.ACCELERATION);
                message.setObject(sensor);
                message.setStringProperty("ModulType", sensor.getClass().getCanonicalName());
                message.setBooleanProperty(Feature.RADIATION.name(), false);
                message.setBooleanProperty(Feature.ACCELERATION.name(), false);
                
                
                System.out.println("Publishing message: " + message.getObject().toString());
                publisher.send(message);
                

                 sensor = new Sensor(i*i+1);
                sensor.addFeature(Feature.RADIATION);
                sensor.addFeature(Feature.ACCELERATION);
                message.setObject(sensor);
                message.setStringProperty("ModulType", sensor.getClass().getCanonicalName());
                message.setBooleanProperty(Feature.RADIATION.name(), false);
                message.setBooleanProperty(Feature.ACCELERATION.name(), false);
                
                
                System.out.println("Publishing message: " + message.getObject().toString());
                publisher.send(message);

                
                
                
            }
            
            
            
            
        } catch (JMSException e) {
            System.out.println("Exception occurred: " +
                    e.toString());
        } finally {
            if (queueConnection != null) {
                try {
                    queueConnection.close();
                } catch (JMSException e) {}
            }
        }
    }
}
